<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
    >
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="dataForm.projectName"
          placeholder="projectName"
        ></el-input>
      </el-form-item>
      <el-form-item label="项目负责人" prop="projectLeaderId">
        <el-select
          v-model="dataForm.projectLeaderId"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in options"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目描述" prop="projectDesc">
        <el-input
          v-model="dataForm.projectDesc"
          placeholder="projectDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="项目时段" prop="dateP">
        <el-date-picker
          v-model="dataForm.dateP"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      options: [],
      dataForm: {
        dateP: "",
        id: 0,
        projectName: "",
        projectDesc: "",
        projectLeaderId: "",
      },
      dataRule: {
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        projectDesc: [
          { required: true, message: "项目描述不能为空", trigger: "blur" },
        ],
        projectLeaderId: [
          {
            required: true,
            message: "项目负责人编号不能为空",
            trigger: "blur",
          },
        ],
        dateP: [
          {
            required: true,
            message: "请选择项目时段",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getPeopleList();
  },
  methods: {
    // 人员列表
    getPeopleList() {
      this.$http({
        url: this.$http.adornUrl("/sys/user/list"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 999,
          roleName: "",
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.options = data.page.list;
        }
      });
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/business/eproject/query`),
            method: "get",
            params: this.$http.adornParams({
              projectId: this.dataForm.id,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.projectName = data.page.records[0].project_name;
              this.dataForm.projectDesc = data.page.records[0].project_desc;
              this.dataForm.projectLeaderId =
                data.page.records[0].project_leader_id;
              this.dataForm.dateP = (
                data.page.records[0].start_date +
                "," +
                data.page.records[0].end_date
              ).split(",");
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/business/eproject/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "get",
            params: this.$http.adornParams({
              projectId: this.dataForm.id,
              projectName: this.dataForm.projectName,
              projectDesc: this.dataForm.projectDesc,
              projectLeaderId: this.dataForm.projectLeaderId,
              startDate: this.dataForm.dateP[0],
              endDate: this.dataForm.dateP[1],
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
